<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 05-03
 * Time: 10:05
 */

namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Base;
use think\Request;

/**
 * Class Payment
 * @package app\api\controller
 * 收银台控制器
 */
class Payment extends Api
{
    /**
     * @var string 日志目录
     */
    protected static $dir = '';

    /**
     * @var array 请求数据
     */
    protected $inputs = [];
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$dir = RUNTIME_PATH.$this->request->controller().DS.$this->request->action().DS;
        $this->inputs = $this->request->getInput();
    }

    /**
     *获取收银方式
     */
    public function get_ids()
    {
        $user = $this->auth->getUserinfo();
        $user_id = $user['id'];
        $type = $this->inputs['type'];
        $order_id = $this->inputs['order_id'];

        $version = $this->inputs['version'];            //版本号
        $mobileType = $this->inputs['mobileType'];    //手机类型  iphone  android

        switch ($type) {
            case 'chongzhi':
                $id = array(12,15);
                break;
            case 'iot':
                $id = array(1,12,15);
                break;
            default :
                $id = array(1,12,15);
        }

        $paymentModel = Base::instance("payment");
        $paymentRows = $paymentModel::search(['id'=>['in',$id],'status'=>0],['field'=>'id,name,class_name,logo','order_by'=>' order asc']);
        $userInfo = array();
        if ($user_id) {

            $userInfo['order_amount'] = 0;
            $orderModel = Base::instance("orders");
            if (!empty($order_id)) {
                $orderRows = $orderModel::search(['id'=>['in',$order_id],'uid'=>$user_id,'orderstatus'=>1],['field'=>'COUNT(id) as cnt,SUM(ordermoney) as ordermoney']);
                $userInfo['order_amount'] = $orderRows[0]['ordermoney']?$orderRows[0]['ordermoney']:'0';
            }


            $userinfo = $user;
            $userInfo['balance'] = $userinfo['money']?(string)$userinfo['money']:'0';
            $userInfo['point'] = $userinfo['point']?(string)$userinfo['point']:'0';
        }
        $dataArray = array();
        foreach ($paymentRows as $v) {
            $dataArray[$v['id']]['id'] = (string)$v['id'];
            $dataArray[$v['id']]['name'] = $v['name'];
            $dataArray[$v['id']]['class_name'] = $v['class_name'];
            $dataArray[$v['id']]['url'] = $this->request->baseUrl().'/uploads/logos'.$v['logo'];
        }
        $this->success('',['data'=>array_values($dataArray),'info'=>$userInfo]);
    }
}